package com.ruoyi.electricity.task;

import com.ruoyi.HKISAPI.service.ISAPIService;
import com.ruoyi.electricity.mapper.ElectricityDataMapper;
import com.ruoyi.electronicFence.mapper.TEfAlarmMapper;
import com.ruoyi.rsikManage.service.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletContext;


@Component
@Getter
@Setter
public class DemonstrateScheduler implements SchedulingConfigurer {

    @Value("${DemonstrateScheduler.updateElectricityHistoryDataCron}")
    private String initialMonitorStatusUpdateCron;
    private boolean isLastReceiveCornErr;
    @Value("${DemonstrateScheduler.updateElectricityHistoryDataCron}")
    private String monitorStatusUpdateCron;
    @Value("${DemonstrateScheduler.switch}")
    private String DemonstrateSchedulerSwitch;
    @Value("${hk.switch}")
    private String HK_SWITCH;
    @Value("${system.name}")
    private String SYSTEM_NAME;

    @Autowired
    JyService jyService;
    @Autowired
    MonitorPointService monitorPointService;
    @Autowired
    MonitorPointHistoryService monitorPointHistoryService;
    @Autowired
    BroadcastService broadcastService;
    @Autowired
    CustomConfigurationService customConfigurationService;
    @Autowired
    ServletContext servletContext;

    @Autowired
    ISAPIService iSAPIService;
    @Autowired
    ElectricityDataMapper electricityDataMapper;
    @Autowired
    TEfAlarmMapper tEfAlarmMapper;
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        if (DemonstrateSchedulerSwitch.equals("true")){
            taskRegistrar.addTriggerTask(() -> {
                updateElectricityHistoryData();
            }, triggerContext -> {
                // 使用CronTrigger触发器，动态修改cron表达式来操作循环规则
                CronTrigger cronTrigger;
                if (isLastReceiveCornErr) {
                    cronTrigger = new CronTrigger(initialMonitorStatusUpdateCron);
                }else {
                    try {
                        cronTrigger = new CronTrigger(monitorStatusUpdateCron);
                    }catch (IllegalArgumentException e) {
                        System.out.println("上次接收的monitorStatusUpdateCron:\""+ monitorStatusUpdateCron + "\" 格式错误，monitorStatusUpdateCron 更新为初始值======");
                        cronTrigger = new CronTrigger(initialMonitorStatusUpdateCron);
                        isLastReceiveCornErr = true;
                    }
                }
                return cronTrigger.nextExecutionTime(triggerContext);
            });
        }

    }
    @Transactional
    public void updateElectricityHistoryData(){

        electricityDataMapper.updateElectricityHistoryData();
        tEfAlarmMapper.updateEfAlarmData();

    }





}
